﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace CuaHangHoa
{
    public partial class dsnhacungcap : Form
    {
        public dsnhacungcap()
        {
            InitializeComponent();
        }


        private void btSua_Click(object sender, EventArgs e)
        {
            try
            {
                Sua_ncc frm = new Sua_ncc();
                frm.RefreshDgv += new Sua_ncc.DoEvent(Load_NCC);
                Nhacungcap_DTO ds = new Nhacungcap_DTO();
                GetDataFromGridViews(ds);
                frm.Loaddata_gv(ds);
                frm.ShowDialog();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Bạn chưa chọn nhà cung cấp !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void GetDataFromGridViews(Nhacungcap_DTO ds)
        {
            int iRow = GridView_NCC.CurrentRow.Index;
            ds.MaNCC = int.Parse(GridView_NCC.Rows[iRow].Cells[0].Value.ToString());
            ds.TenNCC = GridView_NCC.Rows[iRow].Cells[1].Value.ToString();
            ds.SDT = int.Parse(GridView_NCC.Rows[iRow].Cells[2].Value.ToString());
            ds.Email = GridView_NCC.Rows[iRow].Cells[3].Value.ToString();
            ds.Diachi = GridView_NCC.Rows[iRow].Cells[4].Value.ToString();
            ds.Fax = GridView_NCC.Rows[iRow].Cells[5].Value.ToString();
            ds.Website = GridView_NCC.Rows[iRow].Cells[6].Value.ToString();
            ds.Taikhoan = GridView_NCC.Rows[iRow].Cells[7].Value.ToString();
            ds.TinhTrang = GridView_NCC.Rows[iRow].Cells[8].Value.ToString();
            ds.GhiChu = GridView_NCC.Rows[iRow].Cells[9].Value.ToString();
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            Close();
        }
        public void Load_NCC()
        {
            List<Nhacungcap_DTO> ds;
            Nhacungcap_BUS bus = new Nhacungcap_BUS();
            ds = bus.LayDanhSachNCC();

            GridView_NCC.Rows.Clear();
            for (int i = 0; i <= ds.Count - 1; i++)
            {
                GridView_NCC.Rows.Add(ds[i].MaNCC, ds[i].TenNCC, ds[i].SDT, ds[i].Email, ds[i].Diachi, ds[i].Fax, ds[i].Website, ds[i].Taikhoan, ds[i].TinhTrang, ds[i].GhiChu);
            }
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            Them_ncc frm = new Them_ncc();
            frm.RefreshDgv += new Them_ncc.DoEvent(Load_NCC);
            frm.ShowDialog();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            string ma;
            Nhacungcap_BUS bus = new Nhacungcap_BUS();
            DialogResult dialogResult = MessageBox.Show("Bạn muốn xóa !", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                try
                {
                    if (GridView_NCC.RowCount == 0)
                    {
                        MessageBox.Show("Bạn chưa chọn nhà cung cấp !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    foreach (DataGridViewRow row in GridView_NCC.SelectedRows)
                    {
                        ma = row.Cells[0].Value.ToString();
                        bus.XoaNhaCungCap(ma);
                        GridView_NCC.Rows.Remove(row);
                    }
                    MessageBox.Show("Xóa thành công");
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Xóa thất bại");
                }
            }
            else if (dialogResult == DialogResult.No)
            {
                return;
            }
        }

        private void dsnhacungcap_Load(object sender, EventArgs e)
        {
            Load_NCC();
        }

        private void timkiem(string ht)
        {
            List<Nhacungcap_DTO> ds;
            Nhacungcap_BUS bus = new Nhacungcap_BUS();

            if (ht == "")
            {
                ds = bus.LayDanhSachNCC();
            }
            else
            {
                ds = bus.LayDanhSachNCC_Ten(ht);
            }

            GridView_NCC.Rows.Clear();
            for (int i = 0; i <= ds.Count - 1; i++)
            {
                GridView_NCC.Rows.Add(ds[i].MaNCC, ds[i].TenNCC, ds[i].SDT, ds[i].Email, ds[i].Diachi, ds[i].Fax, ds[i].Website, ds[i].Taikhoan, ds[i].TinhTrang, ds[i].GhiChu);
            }
        }

        private void bt_timkiem_Click(object sender, EventArgs e)
        {
            string hoten = txthotenncc.Text;
            timkiem(hoten);
        }

        private void txthotenncc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string hoten = txthotenncc.Text;
                timkiem(hoten);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Lichsugiaodich frm = new Lichsugiaodich();
                Nhacungcap_DTO ds = new Nhacungcap_DTO();
                GetDataFromGridViews(ds);
                frm.Loaddata_gv(ds);
                frm.ShowDialog();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Bạn chưa chọn nhà cung cấp !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void dsnhacungcap_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult ok;
            ok = MessageBox.Show("Bạn có muốn thoát ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ok == DialogResult.No)
            {
                e.Cancel = true;
            }
        }
    }
}
